package com.kx.game.center.servlet;

import com.alibaba.fastjson.JSONObject;
import com.kx.game.center.bean.UserBean;
import com.kx.game.center.global.GSServices;
import com.kx.game.center.serverDistribution.ResponseState;
import com.kx.game.center.serverDistribution.ServerDistributionInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.RedisConnectionFailureException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 获取逻辑服
 */
@WebServlet(urlPatterns = "/transferServlet", asyncSupported = false)
public class TransferServlet extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(TransferServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        JSONObject jsonObjects;
        try {
//            UserBean userBean = GSServices.userManager.getUserInfo(key);
//            if (userBean != null){
//                JSONObject jsonObject = GSServices.localManager.getGameServerInfo(userBean.getServerId());
//                jsonObjects = GSServices.localManager.getJson(
//                        ResponseState.SUCCESS,String.valueOf(jsonObject.get("ip")),String.valueOf(jsonObject.get("gamePort")));
//            }else {
//                jsonObjects = ServerDistributionInterface.ServerDistributionEnum.getByIndex
//                        (GSServices.serverConfig.getDistributionMode()).execute(request, response);
//            }
            jsonObjects = GSServices.localManager.getJson(ResponseState.SUCCESS,"129.204.65.44","8080");
            GSServices.localManager.sendMessage(jsonObjects, response);
        } catch (RedisConnectionFailureException e) {
            jsonObjects = GSServices.localManager.getJson(ResponseState.REDIS_ERROR,null,null);
            GSServices.localManager.sendMessage(jsonObjects,response);
            logger.error("redis server error");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }


}
